import {
  addDepotGoods,
  checkDepotGoodsExist,
  checkDepotGoodsInCompany,
  checkUserInCompany,
  checkUserInCompanyRole,
  subDepotGoods
} from "./utils.js";
import {CompanyRole} from "../roleTypes/CompanyRole.js";
import sequelize from "../models/init.js";
import depotDao from "../Dao/depot.js";
import depotRecordDao from "../Dao/depot_record.js";
import {convertArray, httpResult} from "../utils/utils.js";

// 创建仓库品类 1.添加仓库内容 2.添加入库记录
export const depotGoodsCreate = async (req, res) => {
  let {id, name, unit, cost, amount, companyId, rawMaterial} = req.body;
  await checkUserInCompanyRole(companyId, id, [CompanyRole.CREATOR, CompanyRole.ADMIN]);
  await checkDepotGoodsExist(name, companyId);
  const t = await sequelize.transaction();
  try {
    // 添加仓库内容
    const goods = await depotDao.add(name, unit, amount, cost, rawMaterial, companyId, t);
    // 添加入库记录
    if (Number(amount) > 0) {
      await depotRecordDao.add(goods.id, amount, 0, '创建仓库物品', t);
    }
    await t.commit();
  } catch (e) {
    console.log(e, '创建仓库品类失败');
    await t.rollback();
    throw new Error('创建仓库品类失败');
  }
  res.json(httpResult.success({result: true}));
};

// 仓库物品入库
export const depotGoodsIn = async (req, res) => {
  let {id, amount, companyId, goodsId, remark} = req.body;
  await checkDepotGoodsInCompany(goodsId, companyId);
  await checkUserInCompanyRole(companyId, id, [CompanyRole.CREATOR, CompanyRole.ADMIN]);
  const t = await sequelize.transaction();
  try {
    // 添加入库记录
    await depotRecordDao.add(goodsId, amount, 0, remark, t);
    // 更新仓库内容
    await addDepotGoods(goodsId, amount, t);
    await t.commit();
  } catch (e) {
    console.log(e, '仓库物品入库失败');
    await t.rollback();
    throw new Error('仓库物品入库失败');
  }
  res.json(httpResult.success({result: true}));
};

// 仓库物品出库
export const depotGoodsOut = async (req, res) => {
  let {id, amount, companyId, goodsId, remark} = req.body;
  await checkDepotGoodsInCompany(goodsId, companyId);
  await checkUserInCompanyRole(companyId, id, [CompanyRole.CREATOR, CompanyRole.ADMIN]);
  const t = await sequelize.transaction();
  try {
    // 添加出库记录
    await depotRecordDao.add(goodsId, amount, 1, remark, t);
    // 更新仓库内容
    await subDepotGoods(goodsId, amount, t);
    await t.commit();
  } catch (e) {
    console.log(e, '仓库物品出库失败');
    await t.rollback();
    throw new Error(e.message);
  }
  res.json(httpResult.success({result: true}));
};

// 删除仓库物品
export const depotGoodsDelete = async (req, res) => {
  let {id, companyId, goodsId} = req.body;
  await checkDepotGoodsInCompany(goodsId, companyId);
  await checkUserInCompanyRole(companyId, id, [CompanyRole.CREATOR, CompanyRole.ADMIN]);
  // 判断仓库物品剩余
  const goods = await depotDao.getDepotDetail(goodsId);
  if (Number(goods.amount) !== 0) {
    throw new Error('仓库物品不为空，无法删除');
  }
  // 删除物品
  await depotDao.delete(goodsId);
  res.json(httpResult.success({result: true}));
};

// 查询仓库库存
export const getDepotGoods = async (req, res) => {
  let {id, companyId} = req.body;
  await checkUserInCompany(companyId, id);
  const goodsList = await depotDao.getCompanyDepotList(companyId);
  res.json(httpResult.success({result: true, goodsList}));
};

// 查询仓库某一物品的出入记录
export const getDepotRecord = async (req, res) => {
  let {id, companyId, goodsId} = req.body;
  await checkUserInCompany(companyId, id);
  await checkDepotGoodsInCompany(goodsId, companyId);
  const recordList = await depotRecordDao.queryByGoodsId(goodsId);
  res.json(httpResult.success({result: true, recordList}));
};

// 获取仓库的所有物品出入记录
export const getDepotAllRecord = async (req, res) => {
  let {id, companyId} = req.body;
  await checkUserInCompany(companyId, id);
  // 获取仓库的所有物品ids
  const goodsIdsArr = await depotDao.getCompanyDepotList(companyId);
  const goodsIds = convertArray(goodsIdsArr, 'id');
  console.log(goodsIds);
  // 查询这些id的出入记录
  const recordList = await depotRecordDao.queryByIds(goodsIds);
  res.json(httpResult.success({result: true, recordList}));
};
